package com.ruoyi.youtian.controller;

import com.ruoyi.common.annotation.AutoSetDefaultParams;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.youtian.domain.Equipment;
import com.ruoyi.youtian.domain.vo.TemplatePointBO;
import com.ruoyi.youtian.service.IEquipmentService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 设备Controller
 *
 * @author ruoyi
 * @date 2022-09-07
 */
@RestController
@RequestMapping("/youtian/equipment")
public class EquipmentController extends BaseController
{
    @Autowired
    private IEquipmentService equipmentService;

    /**
     * 查询设备列表
     */
//    @PreAuthorize("@ss.hasPermi('system:equipment:list')")
    @RequestMapping("/list")
    public AjaxResult list( Equipment equipment)
    {
        startPage();
        List<Equipment> list = equipmentService.selectEquipmentList(equipment);
        if (CollectionUtils.isEmpty(list)) {
            return success(null);
        }
        List<Map<String,Object>> resultMapList =new ArrayList<>();
        Map<Long, List<Equipment>> equipmentMap = list.stream().collect(Collectors.groupingBy(Equipment::getEquipmentId));
        equipmentMap.keySet().forEach(k->{
            Map<String,Object> map=new HashMap<>();
            map.put("equipmentName",equipmentMap.get(k).get(0).getEquipmentName());
            map.put("equipmentId",k);
            map.put("pointList",equipmentMap.get(k));
            resultMapList.add(map);
        });
        return success(resultMapList);
    }

    /**
     * 导出设备列表
     */
//    @PreAuthorize("@ss.hasPermi('system:equipment:export')")
    @Log(title = "设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Equipment equipment)
    {
        List<Equipment> list = equipmentService.selectEquipmentList(equipment);
        ExcelUtil<Equipment> util = new ExcelUtil<Equipment>(Equipment.class);
        util.exportExcel(response, list, "设备数据");
    }

    /**
     * 获取设备详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:equipment:query')")
    @GetMapping(value = "/{equipmentId}")
    public AjaxResult getInfo(@PathVariable("equipmentId") Long equipmentId)
    {
        return AjaxResult.success(equipmentService.selectEquipmentByEquipmentId(equipmentId));
    }

    /**
     * 新增设备
     */
//    @PreAuthorize("@ss.hasPermi('system:equipment:add')")
    @Log(title = "设备", businessType = BusinessType.INSERT)
    @PostMapping
    @AutoSetDefaultParams
    public AjaxResult add(@RequestBody Equipment equipment)
    {
        return toAjax(equipmentService.insertEquipment(equipment));
    }

    /**
     * 修改设备
     */
//    @PreAuthorize("@ss.hasPermi('system:equipment:edit')")
    @Log(title = "设备", businessType = BusinessType.UPDATE)
    @RequestMapping(value = "/update")
    @AutoSetDefaultParams
    public AjaxResult edit(@RequestBody Equipment equipment)
    {
        return toAjax(equipmentService.updateEquipment(equipment));
    }

    /**
     * 删除设备
     */
//    @PreAuthorize("@ss.hasPermi('system:equipment:remove')")
    @Log(title = "设备", businessType = BusinessType.DELETE)
	@DeleteMapping("/{equipmentIds}")
    public AjaxResult remove(@PathVariable Long[] equipmentIds)
    {
        return toAjax(equipmentService.deleteEquipmentByEquipmentIds(equipmentIds));
    }

    @GetMapping("/point-equipment-list")
    public AjaxResult pointEquipmentList(Equipment equipment){
        return  AjaxResult.success(equipmentService.selectPointEquipmentList(equipment));
    }
}
